package zy1220.zy5;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;

public class demo {
    public static void main(String[] args) throws Exception {
        while (true){
            System.out.println("【1】账号登陆");
            System.out.println("【2】游客浏览");
            int num = new Scanner(System.in).nextInt();
            switch (num){
                case 1:
                    boolean b = login();
                    if (b==true){
                        return;
                    }else {
                        break;
                    }
                case 2:
                    otherLogin();
                    return;
                default:
                    System.out.println("请输入正确浏览方式！");
                    continue;
            }
        }
    }

    public static void otherLogin() throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date();
        String s = sdf.format(date);
        String str = "游客 "+s+" 进行浏览！\n";
        FileUtils.write(new File("D:\\ideadashuju12\\liu_ya_kai\\src\\zy1220\\zy5\\data.txt"),str,true);
        System.out.println("游客已浏览！");
    }

    public static boolean login() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        FileInputStream f = new FileInputStream("D:\\ideadashuju12\\liu_ya_kai\\src\\zy1220\\zy5\\user.properties");
        Properties prop = new Properties();
        prop.load(f);
        f.close();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String name = sc.nextLine();
        System.out.println("请输入密码");
        String pd = sc.nextLine();
        boolean b = false;
        for (String s : prop.stringPropertyNames()) {
            if (s.equals(name) && prop.get(s).equals(pd)){
                b = true;
                break;
            }
        }
        if (b){
            System.out.println("登录成功");
            Date date = new Date();
            String s = sdf.format(date);
            String str = name+" "+s+" 登录成功！\n";
            FileUtils.write(new File("D:\\ideadashuju12\\liu_ya_kai\\src\\zy1220\\zy5\\data.txt"),str,true);
        }else {
            System.out.println("没有该用户，请重新输入");
            Date date = new Date();
            String s = sdf.format(date);
            String str = name+" "+s+" 登录失败！\n";
            FileUtils.write(new File("D:\\ideadashuju12\\liu_ya_kai\\src\\zy1220\\zy5\\data.txt"),str,true);
            return false;
        }
        return true;
    }
}
